<template>
  <div @scroll="rememberPos" ref="alist" class="scroll-wrapper">
    <van-pull-refresh :success-text="refreshTxt" v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <van-cell v-for="item in list" :key="item.art_id.toString()" :to="`/article/${item.art_id.toString()}`">
          <div class="article_item">
            <h3 class="van-ellipsis">{{item.title}}</h3>
            <div class="img_box" v-if="item.cover.images.length">
              <van-image lazy-load :key="i" v-for="(img,i) in item.cover.images" :class="item.cover.type===1?'w100':'w33'" fit="cover" :src="img" />
            </div>
            <div class="info_box">
              <span>{{item.aut_name}}</span>
              <span>{{item.comm_count}}评论</span>
              <span>{{item.pubdate|relTime}}</span>
              <span v-if="$store.state.user" class="close" @click.stop="openMore(item.art_id.toString())">
                <van-icon name="cross"></van-icon>
              </span>
            </div>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
    <!-- 更多操作弹出层 -->
    <MoreAction v-model="isShowMore" :articleId="articleId" @on-disLibke="delAritcle" @on-reporr="delAritcle" />
    <!-- <MoreAction
      :value="isShowMore"
      @input="isShowMore=$event"
    /> -->
  </div>
</template>

<script>
import { getArticlesById } from '../../../api/article' // 获取文章列表
import MoreAction from './more-action'
export default {
  components: {
    MoreAction
  },
  props: {
    channelId: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      // 文章列表
      list: [],
      // 加载状态
      loading: false,
      // 加载完成
      finished: false,
      // 是否正在刷新
      refreshing: false,
      refreshTxt: '',
      // 分页时间戳
      timestamp: +new Date(),
      // 滚动条高度
      scrollTop: 0,
      // 控制更多悬浮层
      isShowMore: false,
      // articleId
      articleId: ''
    }
  },
  methods: {
    // 自定义事件
    delAritcle () {
      const artInd = this.list.findIndex((item) => item.art_id.toString() === this.articleId)
      this.list.splice(artInd, 1)
    },
    // 上滑加载列表执行
    async onLoad () {
      // 异步更新数据
      const { status, data } = await getArticlesById(this.channelId, this.timestamp)
      if (status === 200) {
        // 追加列表数据
        this.list.push(...data.results)
        // 加载状态结束
        this.loading = false
        // 刷新完成
        this.refreshing = false
        this.refreshTxt = '刷新成功'
        // 数据全部加载完成
        if (data.pre_timestamp) {
          this.timestamp = data.pre_timestamp
        } else {
          this.finished = true
        }
      }
    },
    // 下拉刷新执行
    onRefresh () {
      // 清空列表数据
      if (this.refreshing) {
        this.list = []
      }
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.finished = false
      this.loading = true
      // 设置最新的时间戳
      this.timestamp = Date.now()
      this.onLoad()
    },
    // 记录列表滚动位置
    rememberPos (e) {
      // 存储到实例this上
      this.scrollTop = e.target.scrollTop
      // console.log(e.target.scrollTop)
    },
    // 根据记录滚动到上次浏览位置
    scrollList () {
      this.$nextTick(() => {
        this.$refs.alist.scrollTop = this.scrollTop
      })
    },
    // 打开叉叉，更多操作
    openMore (id) {
      this.isShowMore = true
      this.articleId = id
    }
  }
}
</script>

<style lang="less" scoped>
.article_item {
  h3 {
    font-weight: normal;
    line-height: 2;
  }
  .img_box {
    display: flex;
    justify-content: space-between;
    .w33 {
      width: 33%;
      height: 90px;
    }
    .w100 {
      width: 100%;
      height: 180px;
    }
  }
  .info_box {
    color: #999;
    line-height: 2;
    position: relative;
    font-size: 12px;
    span {
      padding-right: 10px;
      &.close {
        border: 1px solid #ddd;
        border-radius: 2px;
        line-height: 15px;
        height: 12px;
        width: 16px;
        text-align: center;
        padding-right: 0;
        font-size: 8px;
        position: absolute;
        right: 0;
        top: 7px;
      }
    }
  }
}
</style>
